<template>
	<div class="login-bg" :style="'background-image:url(' + bgimg_url + ');'">
		<el-form :model="ruleForm" :rules="rules2" ref="ruleForm" label-position="left" label-width="0px"
			class="demo-ruleForm login-container">
			<h3 class="title">{{admin_name}}</h3>
			<el-form-item prop="account">
				<el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="账号"></el-input>
			</el-form-item>
			<el-form-item prop="checkPass">
				<el-input type="password" v-model="ruleForm.checkPass" auto-complete="off" placeholder="密码"></el-input>
			</el-form-item>
			<el-form-item prop="code" style="line-height: 0px" v-if="codeImage">
				<div class="d-b-c">
					<div class="left-img-input" style="width: auto; margin-bottom: 0">
						<el-input v-model="ruleForm.code" ref="code" placeholder="验证码" class="l-input"
							style="width: 130px"></el-input>
					</div>
					<div class="identifybox" style="height: 30px; flex-shrink: 0;margin-left: 20px;" @click="getData">
						<el-image class="codeImage" style="width: 130px; height: 32px" :src="codeImage" />
					</div>
				</div>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import {
		useUserStore
	} from '@/store';
	import {
		base,
		login
	} from '@/api/user.js'
	// import bgimg from '@/assets/img/login_bg.png';
	const {
		afterLogin
	} = useUserStore();
	export default {
		data() {
			return {
				logining: false,
				bgimg_url: '',
				admin_name: '',
				ruleForm: {
					account: '',
					checkPass: '',
					/*验证码*/
					code: '',
					/*验证码key*/
					codeKey: '',
				},
				rules2: {
					account: [{
						required: true,
						message: '请输入账号',
						trigger: 'blur'
					}],
					checkPass: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}],
					/*验证码*/
					code: [{
						required: true,
						message: "请输入验证码",
						trigger: "blur",
					}, ]
				},
				checked: true,
				codeImage: ''
			};
		},
		created() {
			this.getData();
		},
		methods: {
			/*获取基础配置*/
			getData() {
				let self = this;
				base({}, true)
					.then(res => {
						self.loading = false;
						const data = res.data.settings;
						const codeData = res.data.codeData;
						self.admin_name = data.admin_name;
						if (data.admin_bg_img) {
							self.bgimg_url = data.admin_bg_img;
						} else {
							self.bgimg_url = '';
						}
						if (codeData.codeImage) {
							self.codeImage = codeData.codeImage;
							self.ruleForm.codeKey = codeData.codeKey;
						}
					})
					.catch(error => {
						self.loading = false;
					});
			},
			handleSubmit(ev) {
				var _this = this;
				this.$refs.ruleForm.validate((valid) => {
					if (valid) {
						_this.logining = true;
						var loginParams = {
							username: _this.ruleForm.account,
							password: _this.ruleForm.checkPass,
							code: _this.ruleForm.code,
							codeKey: _this.ruleForm.codeKey,
						};
						login(loginParams, true)
							.then(async (data) => {
								await afterLogin(data);
								_this.logining = false;
								_this.$router.push({
									path: '/Home'
								})
							})
							.catch(error => {
								if (_this.codeImage) {
									this.getData();
								}
								_this.logining = false;
							});
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.login-bg {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background-repeat: no-repeat;
		background-position: center;
		background-size: 100%;
	}

	.login-container {
		box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
		-webkit-border-radius: 5px;
		border-radius: 5px;
		-moz-border-radius: 5px;
		background-clip: padding-box;
		margin: 180px auto;
		width: 350px;
		padding: 35px 35px 15px 35px;
		background: #323a4a;
		border: 1px solid #384050;
		box-shadow: 0 0 12px 0 rgba(0, 0, 0, .4);

		.title {
			margin: 0px auto 30px auto;
			text-align: center;
			color: #FFFFFF;
			font-size: 16px;
		}

		.remember {
			margin: 0px 0px 35px 0px;
		}
	}
</style>